<script setup>
import { onMounted, ref, onUnmounted } from 'vue'
import commonModel from '@/http/modules/common.js'
import Utility from '@/utils/utility'
import { useRouter } from 'vue-router'
import { Overlay, showToast } from 'vant'
import * as _ from 'lodash'
const prize_btn_unactive_img = `${Utility.getOssStaticDomain()}/cross/prize/prize_btn.png`
const prize_btn_active_img = `${Utility.getOssStaticDomain()}/cross/prize/prize_btn_active.png`
const prize_gift_bg = `${Utility.getOssStaticDomain()}/cross/prize/prize_gift_bg.png`
const prize_rule_bg = `${Utility.getOssStaticDomain()}/cross/prize/prize_rule_bg.png`
const paramsCode = Utility.getSearchParams().code
const luckInstance = ref(null)
const prizeInfo = ref({})
const prizeList = ref([])
const router = useRouter()
const isDrawing = ref(false)
const scrollList = ref([])
const prizeShow = ref(false)
const obtainedPrizeInfo = ref({}) //获得奖品的信息
const prizeCode = ref('')
const loadedOpenPrizeBg = ref(false)
onMounted(() => {
  luckInstance.value = new LuckDraw()
  getPrizeInfo()
})
function getPrizeInfo() {
  commonModel
    .gatewayApi({
      service: 'cb_trade_draw_info',
      bizContent: {
        code: paramsCode,
      },
      version: '1.0.0',
    })
    .then((result) => {
      if (result) {
        prizeInfo.value = result
        prizeList.value = getOssPriceList(result.prizeList)
        scrollList.value = result.showRecordList || []
        Utility.imgPreLoad(`${Utility.getOssStaticDomain()}${prizeInfo.value.headUrl}`,`${Utility.getOssStaticDomain()}${prizeInfo.value.headUrl}`).then(()=>loadedOpenPrizeBg.value=true)
      }
    })
}
function getOssPriceList(list) {
  if (!list || list.length === 0) return []
  return list.map((v) => ({
    ...v,
    showUrl: `${Utility.getOssStaticDomain()}${v.imgUrl}`,
  }))
}
const _throttle_startRotate = _.throttle(startRotate, 1000, {
  leading: true,
  trailing: false,
})
onUnmounted(() => {
  _throttle_startRotate.cancel()
})
function startRotate() { 
  if (!luckInstance.value || !prizeInfo.value.count) return
  isDrawing.value = true
  commonModel
    .gatewayApi({
      service: 'cb_trade_draw_submit',
      bizContent: {
        code: paramsCode,
      },
      version: '1.0.0',
    })
    .then((result) => {
      if (result) {
        const _index = prizeList.value.findIndex(
          (v) => v.awardCode === result.awardCode && v.code === result.code
        )
        if (_index > -1) {
          Utility.imgPreLoad(prize_gift_bg,'prize_gift_bg')
          Utility.imgPreLoad(prize_rule_bg,'prize_rule_bg')
          prizeCode.value = result.code
          luckInstance.value._init({
            rotateId: '.rui-luck-prize',
            activeClass: 'rui-active',
            luckNumber: _index,
            typeNumber: 8,
            time: 2000,
            type: 'prize',
            circleNumber: 6,
            success: function () {
              prizeShow.value = true
              obtainedPrizeInfo.value = prizeList.value[_index]
              getPrizeInfo()
            },
          })
        }
      }
    })
}
function toRules() {
  router.push(`/prizeRule?code=${paramsCode}`)
}
function toMyPrize() {
  commonModel
    .gatewayApi({
      service: 'cb_trade_draw_record',
      bizContent: {
        code: paramsCode,
      },
      version: '1.0.0',
    })
    .then((result) => {
      if (result) {
        if (result.length > 0) {
          router.push(`/myPrize?code=${paramsCode}`)
        } else {
          showToast('暂无奖品信息')
        }
      }
    })
}
</script>
<template>
  <div class="rui-luck-draw">
    <Overlay :show="prizeShow">
      <div class="overlay-wrapper">
        <div class="overlay-content-wrapper" :style="{backgroundImage:`url(${prize_gift_bg})`}">
          <div class="overlay-content">
            <img
              :style="{
                width: ['P_001', 'P_002', 'P_007'].includes(prizeCode) ? '0.74rem' : '1.4rem',
                marginBottom: ['P_001', 'P_007'].includes(prizeCode) ? '' : '0.2rem',
              }"
              :src="`${Utility.getOssStaticDomain()}${
                obtainedPrizeInfo.imgUrl
              }`"
              class="prize-img"
            />
            <p class="prize-text">恭喜您获得{{ obtainedPrizeInfo.name }}!</p>
            <div class="prize-desc">
              <p class="top">*奖品请以实际为准</p>
              <p class="bottom">请前往“我的奖品”内查看领取方式</p>
            </div>
          </div>
          <span class="over-close" @click="prizeShow = false"></span>
        </div>
      </div>
    </Overlay>
    <div
      class="rui-luck-content"
      :style="{
        backgroundImage: loadedOpenPrizeBg ?  `url(${Utility.getOssStaticDomain()}${prizeInfo.headUrl})` :`url(${Utility.getOssStaticDomain()}/cross/prize/cross_open_prize_bg_low.png)` 
      }"
    >
      <p class="prize-count" v-if="prizeInfo.code">
        {{
          prizeInfo.countDesc
            ? prizeInfo.countDesc
            : `您有  ${prizeInfo.count || 0}  次抽奖资格`
        }}
      </p>
      <div class="rui-luck-box">
        <div
          class="rui-luck-prize"
          :prize="index + 1"
          v-for="(item, index) in prizeList.slice(0, 4)"
          :key="index"
        >
          <img class="prize_url" :src="item.showUrl" alt="" />
          <p class="prize_text">{{ item.name }}</p>
        </div>
        <div
          v-if="prizeInfo.headUrl"
          class="rui-pointer"
          @click="_throttle_startRotate"
          :style="{
            backgroundImage: `url(${
              prizeInfo.count > 0 &&
              !isDrawing &&
              prizeInfo.status === '20' &&
              prizeInfo.gmtEnable
                ? prize_btn_active_img
                : prize_btn_unactive_img
            })`,
          }"
        ></div>
        <div
          class="rui-luck-prize"
          :prize="index + 5"
          v-for="(item, index) in prizeList.slice(4)"
          :key="index"
        >
          <img class="prize_url" :src="item.showUrl" alt="" />
          <p class="prize_text">{{ item.name }}</p>
        </div>
      </div>
      <div class="prize-srcoll" v-if="scrollList.length">
        <div
          class="prize-srcoll-allWidth"
          :style="{
            // '--allWidth': `${3.3 * (scrollList.length + 1)}rem`,
            '--allHeight': `${0.16 * (scrollList.length + 1)}rem`,
            // '--moveSlideshowWidth': `-${3.3 * scrollList.length}rem`,
            '--moveSlideshowHeight': `-${0.16 * scrollList.length}rem`,
            '--srcollTimer': `${scrollList.length * 0.5}s`,
          }"
        >
          <p v-for="(item, index) in scrollList" :key="index">
            {{ item }}
          </p>
          <p>
            {{ scrollList[0] }}
          </p>
        </div>
      </div>
      <div class="prize-footer">
        <div class="btn-rule" @click="toRules">活动规则</div>
        <div class="btn-prize" @click="toMyPrize">我的奖品</div>
      </div>
    </div>
  </div>
</template>
<style scoped lang="scss">
.overlay-wrapper {
  display: flex;
  margin-top: 1.18rem;

  justify-content: center;
  height: 100%;
  .overlay-content-wrapper {
    width: 3.15rem;
    height: 3.4rem;
    background-size: 100%;
    padding: 0.22rem 0.2rem;
    display: flex;
    justify-content: center;
    position: relative;
    .over-close {
      background-image: url('@/assets/img/prize/prize_close_icon.png');
      width: 0.28rem;
      height: 0.28rem;
      background-size: 100%;
      position: absolute;
      bottom: -0.53rem;
    }
    .overlay-content {
      position: absolute;
      text-align: center;
      bottom: 0.19rem;
      .prize-img {
        margin-top: 0.2rem;
        // width: 1.4rem;
      }
      .prize-text {
        color: #953920;
        font-size: 0.18rem;
        margin: 0.1rem 0;
      }
      .prize-desc {
        color: #999999;
        font-size: 0.14rem;
      }
    }
  }
}
.rui-luck-draw {
  height: calc(100vh - 0.46rem);
}
.rui-luck-content {
  width: 100%;
  height: 100%;
  background: no-repeat;
  background-size: 100%;
  position: relative;
  .prize-count {
    padding: 0 0.28rem;
    white-space: nowrap;
    background: #fdd86c;
    border-radius: 0.2rem;
    position: absolute;
    top: 0.76rem;
    left: 50%;
    transform: translateX(-50%);
    font-weight: 400;
    font-size: 0.12rem;
    text-align: center;
    color: #f1423c;
  }
  .prize-srcoll {
    p{
      height: .16rem;
    }
    position: absolute;
    top: 5rem;
    left: 50%;
    transform: translateX(-50%);
    flex-wrap: nowrap;
    white-space: nowrap;
    color: #bd1710;
    // width: 3.3rem;
    height: 0.16rem;
    font-size: 0.12rem;
    // overflow-x: hidden;
    text-align: center;
    overflow-y: hidden;
    &-allWidth {
      -webkit-animation: moveSlideshow var(--srcollTimer) linear infinite;
      // width: var(--allWidth);
      height: var(--allHeight);
      > p {
        width: 3.3rem;
        // float: left;
      }
    }
  }
  @-webkit-keyframes moveSlideshow {
    0% {
      transform: translateX(0);
      -webkit-transform: translateX(0);
    }
    100% {
      // -webkit-transform: translateX(var(--moveSlideshowWidth));
      transform: translateY(var(--moveSlideshowHeight));
      -webkit-transform: translateY(var(--moveSlideshowHeight));
    }
  }
  .prize-footer {
    position: absolute;
    // bottom: 1rem;
    top: 5.4rem;
    width: 100%;
    display: flex;
    justify-content: center;
    .btn-rule {
      margin-right: 0.1rem;
    }
    .btn-rule,
    .btn-prize {
      width: 1.53rem;
      text-align: center;
      background: linear-gradient(180deg, #f9776f 0%, #f1423c 100%);
      border-radius: 0.2rem;
      padding: 0.07rem 0.28rem;
      color: #ffffff;
      font-size: 0.2rem;
    }
  }
  @media screen and (max-height: 667px) {
    .prize-footer {
      // bottom: 0.2rem;
      top: 5.4rem;
    }
    .prize-srcoll {
      top: 5rem;
    }
  }
}
.rui-luck-box {
  width: 2.6rem;
  height: 2.36rem;
  position: absolute;
  top: 2.14rem;
  left: 0.58rem;
}

.rui-luck-box div {
  background-color: #fdf2ef;
  width: 32%;
  height: 32%;
  text-align: center;
  box-sizing: border-box;
  border-radius: 0.08rem;
  font-size: 30px;
  position: absolute;
  display: flex;
  display: -webkit-flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  padding: 0.06rem 0.1rem;
  .prize_url {
    height: 0.32rem;
  }
  .prize_text {
    @include moreWay;
    // margin-top: 0.04rem;
    font-size: 0.1rem;
    color: #953920;
  }
}
.rui-luck-box div[prize='1'] {
  top: 0;
  left: 0;
  transform: translate(0, 0);
}

.rui-luck-box div[prize='2'] {
  top: 0;
  left: 50%;
  transform: translate(-50%, 0);
}

.rui-luck-box div[prize='3'] {
  top: 0;
  right: 0;
  transform: translate(0, 0);
}

.rui-luck-box div[prize='4'] {
  top: 50%;
  right: 0;
  transform: translate(0, -50%);
}

.rui-luck-box div[prize='5'] {
  bottom: 0;
  right: 0;
  transform: translate(0, 0);
}

.rui-luck-box div[prize='6'] {
  bottom: 0;
  left: 50%;
  transform: translate(-50%, 0);
}

.rui-luck-box div[prize='7'] {
  bottom: 0;
  left: 0;
  transform: translate(0, 0);
}

.rui-luck-box div[prize='8'] {
  top: 50%;
  left: 0;
  transform: translate(0, -50%);
}

.rui-luck-box div.rui-active {
  background: #f9db9f;
}

.rui-pointer {
  background-size: 100%;
  background-repeat: no-repeat;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  border: none !important;
}
</style>
